import time
import unittest

from common.decorators import mkdir_classname, log4gs
from common.functions import save_screenshot
from common.settings import TAG
from pages_example.loginPage import LoginPage


@log4gs
@mkdir_classname(TAG)
class LoginTest(unittest.TestCase):
    """登录功能测试"""
    @classmethod
    def setUpClass(cls):
        cls.log.debug('='*30 + '开始登录功能测试' + '='*30)
        cls.login_page = LoginPage('/login', is_login_page=True)

    @classmethod
    def tearDownClass(cls):
        cls.login_page.driver.quit()

    def setUp(self):
        self.log.debug('>>>>>开始执行用例：{} {}<<<<<'.format(self.id(), self.shortDescription()))
        self.login_page.account.clear()
        self.login_page.password.clear()

    def tearDown(self):
        self.login_page.account.clear()
        self.login_page.password.clear()

    def test_wrong_account(self):
        """错误用户名登录"""
        self.login_page.account.send_keys('gosuns')
        self.login_page.password.send_keys('123456')
        self.login_page.login_btn.click()
        time.sleep(3)
        message = self.login_page.get_text('.ivu-message')
        save_screenshot(self, '错误用户名')
        self.assertTrue('用户认证失败，用户或密码错误' in message)

    def test_wrong_password(self):
        """错误密码登录"""
        self.login_page.account.send_keys('gosun')
        self.login_page.password.send_keys('1234567')
        self.login_page.login_btn.click()
        time.sleep(1)
        message = self.login_page.get_text('.ivu-message')
        save_screenshot(self, 'd.png')
        self.assertFalse('用户认证失败，用户或密码错误' in message)


if __name__ == '__main__':
    unittest.main()